# coding: utf-8
import app.tools as tool
from PyQt4 import QtCore, QtGui, Qt
from PyQt4.QtGui import QTableWidget, QTableWidgetItem, QAbstractItemView


class TableWidget(QTableWidget):

    NoEditTriggers = QtGui.QAbstractItemView.NoEditTriggers
    SelectColumns = QtGui.QAbstractItemView.SelectColumns
    SelectedClicked = QtGui.QAbstractItemView.SelectedClicked
    SelectItems = QtGui.QAbstractItemView.SelectItems
    SelectRows = QtGui.QAbstractItemView.SelectRows
    SingleSelection = QtGui.QAbstractItemView.SingleSelection

    def __init__(self, *__args):
        QTableWidget.__init__(self, *__args)

    def init_ui(self):
        self.setSelectionMode(QAbstractItemView.SingleSelection)
        self.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.setAlternatingRowColors(True)
        horizontal_header = self.horizontalHeader()
        horizontal_header.setStretchLastSection(True)
        horizontal_header.setHighlightSections(False)
        # 设置表头不可点击（默认点击后进行排序）
        horizontal_header.setClickable(False)
        # 设置表头第一列的宽度为150
        horizontal_header.resizeSection(0, 150)
        # 设置表头的高度
        horizontal_header.setFixedHeight(25)
        font = horizontal_header.font()
        font.setBold(True)
        horizontal_header.setFont(font)
        horizontal_header.setResizeMode(Qt.QHeaderView.Fixed)
        # 设置垂直头不可见
        self.verticalHeader().setVisible(False)
        # 设置不显示格子线
        # self.setShowGrid(False)
        # 设置无边框
        self.setFrameShape(QtGui.QFrame.NoFrame)
        # 设置行高
        # hash_table_vertical_header = self.verticalHeader()
        # hash_table_vertical_header.setDefaultSectionSize(10)
        # hash_table_vertical_header.setResizeMode(QtGui.QHeaderView.ResizeToContents)
        # 单选
        # self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        # 设置选择行为时每次选择一行
        # self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        # 设置不可编辑
        # self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        # 设置选中背景色
        # self.setStyleSheet("selection-background-color:lightblue;")

    def set_object_name(self, name):
        self.setObjectName(tool._from_utf8(name))

    def set_column_count(self, count):
        self.setColumnCount(count)

    def set_column_width(self, index, width):
        self.setColumnWidth(index, width)

    def set_vertical_header_item(self, index, item):
        self.setVerticalHeaderItem(index, item)

    def set_horizontal_header(self, index, text=None, icon=None):
        item = TableWidgetItem()
        if text:
            item.setText(tool._from_utf8(text))
        if icon:
            item.setIcon(icon)
        self.setHorizontalHeaderItem(index, item)

    def set_horizontal_header_item(self, index, item):
        self.setHorizontalHeaderItem(index, item)

    def insert_row(self, row):
        self.insertRow(row)

    def row_count(self):
        return self.rowCount()

    def set_row_count(self, count):
        self.setRowCount(count)

    def vertical_scroll_bar(self):
        return self.verticalScrollBar()

    def set_item(self, row, column, text):
        item = TableWidgetItem()
        item.setText(tool._from_utf8(str(text)))
        self.setItem(row, column, item)
        return item

    def current_row(self):
        return self.currentRow()

    def set_current_item(self, item):
        self.setCurrentItem(item)

    def remove_row(self, row):
        self.removeRow(row)

    def select_row(self, row):
        self.selectRow(row)

    def set_selection_mode(self, mode):
        self.setSelectionMode(mode)

    def set_selection_behavior(self, type):
        self.setSelectionBehavior(type)

    def set_edit_triggers(self, triggers):
        self.setEditTriggers(triggers)


class TableWidgetItem(QTableWidgetItem):

    def __init__(self, *__args):
        QTableWidgetItem.__init__(self, *__args)

